package com.prs.crm.action.fs;

import java.io.File;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.fs.BankAccount;
import com.prs.crm.domain.fs.SalarySettle;
import com.prs.crm.domain.hr.Payroll;
import com.prs.crm.domain.hr.PayrollBill;
import com.prs.crm.service.fs.BankAccountService;
import com.prs.crm.service.fs.SalarySettleService;
import com.prs.crm.service.hr.PayrollBillService;
import com.prs.crm.service.hr.PayrollService;

public class SalarySettleAction extends BaseAction{
	private SalarySettle salarySettle;
	private SalarySettleService salarySettleService;
	private PaginationSupport pagination;
	private Integer[] ids;
	
	private Payroll payroll;
	private PayrollService payrollService;
	private BankAccountService bankAccountService;
	private PayrollBillService payrollBillService;
	
	private File upload;
	private String uploadFileName;
	private String bank;
	
	private Collection<BankAccount> bankAccounts;
	/**
	 * 实发总计
	 */
	private Double authenticValue;
	/**
	 * 增项总计
	 */
	private Double increaseValue;
	/**
	 * 减项总计
	 */
	private Double reduceValue;
	
	@Action("listSettlePayroll")
	public String listSettlePayroll() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Payroll.class);
		criteria.add(Restrictions.eq("state", Constrains.PAYROLL_ISSUE));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPayrollService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editSettlePayroll")
	public String editSettlePayroll() {
		calculatePayroll(payroll);		
		
		bankAccounts = this.getBankAccountService().getAllBankAccount();
		return SUCCESS;
	}

	private void calculatePayroll(Payroll payroll) {
		reduceValue = 0.0;
		increaseValue = 0.0;
		List<PayrollBill> increaseItems = this.getPayrollBillService().getIncreaseItem(payroll);
		List<PayrollBill> reduceItems = this.getPayrollBillService().getReduceItem(payroll);
		
		for(PayrollBill bill : increaseItems) {
			increaseValue += bill.getValue();
		}
		for(PayrollBill bill : reduceItems) {
			reduceValue += bill.getValue();
		}
		
		authenticValue = increaseValue - reduceValue;
	}
	
	@Action("listSalarySettle")
	public String listSalarySettle() {
		return SUCCESS;
	}
	@Action("listSalarySettleByBank")
	public String listSalarySettleByBank(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(SalarySettle.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		if(bank != null){
			if(!"all".equals(bank)){
				bank = bank.split(",")[0];
				if(ToolKits.isInteger(bank)){
					criteria.createAlias("bankAccount", "ba");
					criteria.add(Restrictions.eq("ba.id", Integer.parseInt(bank)));
				}
			}
		}
		this.setPagination(this.getSalarySettleService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("alterSalarySettle")
	public String alterSalarySettle() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(SalarySettle.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSalarySettleService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Clew(results={"success"})
	@Action(value="submitSalarySettle",results={@Result(name="success",type="redirect",location="listSettlePayroll")})
	public String submitSalarySettle() {
		salarySettle.setPayroll(payroll);
		this.getSalarySettleService().submitSalarySettle(salarySettle, upload, uploadFileName);
		this.setPrompt("提交工资结算单成功");
		return SUCCESS;
	}	
	
	@Action(value="removeSalarySettle",results={@Result(name="success",type="redirect",location="alterSalarySettle"),
			@Result(name="error",type="chain",location="alterSalarySettle")})
	public String removeSalarySettle() {
		if(ids == null) {
			this.addActionError("请选中要删除的工资结算单");
			return ERROR;
		}
		this.getSalarySettleService().batchRemove(ids);
		return SUCCESS;
	}
	
	@Action("viewSalarySettle")
	public String viewSalarySettle() {
		calculatePayroll(salarySettle.getPayroll());		
		return SUCCESS;
	}
	
	public SalarySettle getSalarySettle() {
		return salarySettle;
	}
	public void setSalarySettle(SalarySettle salarySettle) {
		this.salarySettle = salarySettle;
	}
	public SalarySettleService getSalarySettleService() {
		return salarySettleService;
	}
	public void setSalarySettleService(SalarySettleService salarySettleService) {
		this.salarySettleService = salarySettleService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setPayroll(Payroll payroll) {
		this.payroll = payroll;
	}

	public Payroll getPayroll() {
		return payroll;
	}

	public void setPayrollService(PayrollService payrollService) {
		this.payrollService = payrollService;
	}

	public PayrollService getPayrollService() {
		return payrollService;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload() {
		return upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setBankAccountService(BankAccountService bankAccountService) {
		this.bankAccountService = bankAccountService;
	}

	public BankAccountService getBankAccountService() {
		return bankAccountService;
	}

	public void setBankAccounts(Collection<BankAccount> bankAccounts) {
		this.bankAccounts = bankAccounts;
	}

	public Collection<BankAccount> getBankAccounts() {
		return bankAccounts;
	}

	public void setAuthenticValue(double authenticValue) {
		this.authenticValue = authenticValue;
	}

	public double getAuthenticValue() {
		return authenticValue;
	}

	public double getIncreaseValue() {
		return increaseValue;
	}

	public void setIncreaseValue(double increaseValue) {
		this.increaseValue = increaseValue;
	}

	public double getReduceValue() {
		return reduceValue;
	}

	public void setReduceValue(double reduceValue) {
		this.reduceValue = reduceValue;
	}

	public void setPayrollBillService(PayrollBillService payrollBillService) {
		this.payrollBillService = payrollBillService;
	}

	public PayrollBillService getPayrollBillService() {
		return payrollBillService;
	}

	public String getBank() {
		return bank;
	}

	public void setBank(String bank) {
		this.bank = bank;
	}	
	
}
